#include"head.h"

//新建子进程
int init_pool(struct work_status* workarr,int num)
{
    for(int i=0;i<num;i++)
    {
        int local[2];

        int ret=socketpair(AF_LOCAL,SOCK_STREAM,0,local);
        ERROR_CHECK(ret,-1,"socketpair");

        pid_t pid=fork() ;

        if(pid==0)
        {
            //子进程的逻辑
            sleep(1);
            close(local[0]);

            //子进程工作核心
            work(local[1]);
        }
        else
        {
            workarr[i].local_socket=local[0];
            printf("pool.c:local:%d\n",local[0]);
            close(local[1]);
            workarr[i].pid=pid;
            workarr[i].status=FREE;
        }
    }
    return 0;
}

//收到来自客户端的任务把他交给子进程
int give_son_do(int net_fd, struct work_status* workarr,int num,int flag)
{


    printf("send flag:pool.c:%d\n\n",flag);
    for(int i=0;i<num;i++)
    {
        if(workarr[i].status==FREE)
        {
            write_net_fd(workarr[i].local_socket, &net_fd, flag);
            workarr[i].status=BUSY;
            break;
        }
    }
    return 0;
}






